/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.tordroid.core.adapter;

/**
 *
 * @author Christian Simon
 */
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.SpinnerAdapter;
import android.widget.TextView;
import de.tordroid.R;
import de.tordroid.model.IconEntry;
import java.util.List;

public class IconSpinnerAdapter extends BaseAdapter implements
        SpinnerAdapter {

    private final List<IconEntry> content;
    private final Activity activity;

    public IconSpinnerAdapter(List<IconEntry> content,
            Activity activity) {
        super();
        this.content = content;
        this.activity = activity;
    }

    public int getCount() {
        return content.size();
    }

    public IconEntry getItem(int position) {
        return content.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public int getPosition(String arg) {
        int position = 0;
        int i = 0;
        for (IconEntry entry : content) {
            if (entry.getData().equals(arg)) {
                position = i;
                break;
            }
            i++;
        }
        return position;
    }

    public View getView(int position, View convertView,
            ViewGroup parent) {
        final LayoutInflater inflater = activity.getLayoutInflater();   // default layout inflater
        final View spinnerEntry = inflater.inflate(
                R.layout.icon_spinner, null);                                // initialize the layout from xml
        final TextView label = (TextView) spinnerEntry.findViewById(R.id.spinner_label);
        final ImageView icon = (ImageView) spinnerEntry.findViewById(R.id.spinner_icon);
        IconEntry currentEntry = content.get(position);
        if (currentEntry.getTitelRes() == 0) {
            label.setText(currentEntry.getTitel());
        } else {
            label.setText(currentEntry.getTitelRes());
        }
        icon.setImageResource(currentEntry.getIcon());
        return spinnerEntry;
    }
}
